package cn.bugstack.springframework.beans.factory.support;

import cn.bugstack.springframework.beans.BeansException;
import cn.bugstack.springframework.beans.factory.config.BeanDefinition;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;

import java.lang.reflect.Constructor;

/**
 * @param
 * @author oyz
 * @version 1.0.4
 * @description: TODO
 * @date 2022/8/7 17:58
 */
public class CglibSubclassingInstantiationStrategy implements InstantiationStrategy{


    @Override
    public Object instantiate(BeanDefinition beanDefinition, String beanName, Constructor constructor) throws BeansException {
        // 允许为非java类型创建一个java代理,Enhancer动态创建给定类的子类并且拦截代理类的所有方法,和JDK动态代理不一样的是不管是接口还是类它都能正常工作
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(beanDefinition.getBeanClass());
        enhancer.setCallback(new NoOp(){
            @Override
            public int hashCode() {
                return super.hashCode();
            }
        });

        return null;
    }
}
